// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'// 引入vue-router
import store from './store'// 引入vuex
import axios from 'axios'// axios
// 全局配置
import '@/assets/css/base.css'// 自定义全局样式
import 'element-ui/lib/theme-chalk/index.css'// element-ui样式

// 第三方包
import ElementUI from 'element-ui'
Vue.use(ElementUI)
Vue.config.productionTip = false
axios.defaults.withCredentials = false
// axios.defaults.timeout = 6000;
Vue.prototype.$ajax = axios

// http response 拦截器
axios.interceptors.response.use(
  (response) => {
    return response
  }, function (error) {
    const path = window.location.href
    if (error.response) {
      const status = error.response.status
      var reg = RegExp(/login/)
      if (path.split(':')[2].match(reg)) {
        return error.response
      }
      // 如果认证失败 表明 token过期, 跳转到登录页面重新登录
      else if (status === 401) {
        router.replace('/login')
      } else {
        return error.response
      }
    } else { // 超时处理
      return {'status': 600, 'message': '超时', 'url': path}
    }
  }
)

function setCurrentLocation(path){
  var path_array = path.split('/')
  if (path_array[1].length==0) {
    store.commit('setActiveNav', "1") // 设置菜单栏活动目录
  } else {
    store.commit('setActiveNav', Config['navConfig'][path_array[1]]) // 设置菜单栏活动目录
  }
  
  if (path != '/') {
    var location_name = []
    path_array.forEach((v)=>{
      var name = Config['language'][v]
      if (name != 'undefined'){
        location_name.push(name)
      }
    })
    store.commit('setMenuName', location_name.join('/'))
  } else {
    store.commit('setMenuName', '项目看板')
  }
  
}

router.beforeEach((to, from, next) => {
  setCurrentLocation(to.path)
  // 防止刷新后vuex里丢失token
  store.commit('getToken')
  // 防止刷新后vuex里丢失标签列表tagList
  store.commit('getMenu')
  let token = store.state.user.token
  // 过滤登录页，因为去登陆页不需要token(防止死循环)
  if (!token && to.name !== 'login') {
    next({name: 'login'})
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  created () {
    store.commit('addMenu', router)
  }
})
